home *** CD-ROM | disk | FTP | other *** search
- /*
- * CSignature.c
- * Copyright © 1993 Apple Computer Inc.
- * All Rights Reserved
- *
- * This is a Think C class library definition that permits
- * any Think C object to have an attached digital signature.
- * Methods allow you to attach, save, restore, and verify
- * signatures.
- *
- * CSignature is a base class with two sub-classes,
- * CSignedDataFile and CSignedObject. The base class
- * contains methods that are needed for all Digital
- * Signature processing.
- *
- * CSignedDataFile signs entire files. It would normally
- * be called from a CDocument class.
- *
- * CSignedObject signs individual objexts. It might be
- * called from, perhaps, a spreadsheet that needs to
- * sign the contents of individual cells.
- *
- * Note: errors are returned through the Think Class
- * Library "Failure" routines.
- */
- #include "CSignature.h"
- #include "SIGStatusManager.h"
- #include <OCEErrors.h>
- #include <OCE.h>
- #include <DigitalSignature.h>
- #include <Exceptions.h>
- #include <Global.h>
-
- /*
- * This is the only instance of a signature context pointer.
- */
- SIGContextPtr gSIGContextPtr;
-
- /*
- * ISignature is called when the object is created. Note
- * that it succeeds even if the Digital Signature Manager
- * is not available. It is the application's responsibility
- * to avoid calling any methods that call Digital Signature
- * Toolbox functions.
- */
- void
- CSignature::ISignature(void)
- {
- itsStatusManager = NULL;
- }
-
- /*
- * Dispose is called when the object is destroyed.
- */
- void
- CSignature::Dispose(void)
- {
- DisposeDefaultStatusProc();
- ForgetObject(itsStatusManager);
- DisposeSignerContext();
- inherited::Dispose();
- }
-
- /*
- * Create a signature context. ContextType should be one of the
- * following values. It is needed for UpdateMenus and similar
- * to enable/disable the ShowSigner command.
- * kSIGSign Signing
- * kSIGVerify Verify
- * kSIGDigest Digest
- */
- void
- CSignature::NewContext(
- unsigned long contextType
- )
- {
- DisposeSignerContext();
- FailOSErr(SIGNewContext(&gSIGContextPtr));
- FailNIL(gSIGContextPtr);
- itsContextType = contextType;
- }
-
- /*
- * Fail if there is no context.
- * kSIGContextPrepareErr There is no current context.
- */
- void
- CSignature::CheckForContext(void)
- {
- if (gSIGContextPtr == NULL)
- FailOSErr(kSIGContextPrepareErr);
- }
-
- /*
- * Return the context type, or zero if there is no context.
- */
- unsigned long
- CSignature::GetContextType(void)
- {
- if (gSIGContextPtr == NULL)
- return (0);
- else {
- return (itsContextType);
- }
- }
-
- /*
- * SignPrepare creates a new signature context and either
- * reads a specified signature file or prompts the user
- * for a signature. All errors (including userCanceled)
- * exit via Failure. On success, it returns the
- * size of the signature record. The parameters are as follows:
- *
- * signerFile If not NULL, this is the file containing
- * a user's signature file. If NULL, the
- * method will prompt to let the user
- * select a signature file.
- * prompt Normally "\p", this may be set to a
- * user-specified prompt string.
- */
- Size
- CSignature::SignPrepare(
- const FSSpec *signerFile,
- ConstStr255Param prompt
- )
- {
- Size signatureSize;
-
- NewContext(kSIGSign);
- TRY {
- FailOSErr(SIGSignPrepare(
- gSIGContextPtr,
- signerFile,
- prompt,
- &signatureSize
- ));
- }
- CATCH {
- DisposeSignerContext();
- }
- ENDTRY;
- return (signatureSize);
- }
-
- /*
- * After verifying a signature, an application can call
- * ShowSigner to display the certificate information.
- */
- void
- CSignature::ShowSigner(
- ConstStr255Param prompt
- )
- {
- CheckForContext();
- InitCursor();
- FailOSErr(SIGShowSigner(gSIGContextPtr, prompt));
- }
-
- /*
- * GetSignerInfo copies information about the signer
- * to a user-provided buffer. You may call this
- * after you have signed or verified a file, and
- * before you have disposed of the signature context.
- * (Untested)
- */
- void
- CSignature::GetSignerInfo(
- SIGSignerInfo *signerInfo
- )
- {
- CheckForContext();
- FailOSErr(SIGGetSignerInfo(gSIGContextPtr, signerInfo));
- }
-
- /*
- * Get information about one of the certificates in the signature
- * certificate chain. On TRUE return, the information has been stored
- * into the user's buffer. On FALSE return, the certificate index
- * parameter is out of range. Other errors exit through the failure
- * mechanism.
- * (Untested)
- */
- Boolean
- CSignature::GetCertInfo(
- unsigned long certIndex,
- SIGCertInfo *certInfo
- )
- {
- OSErr status;
-
- CheckForContext();
- status = SIGGetCertInfo(gSIGContextPtr, certIndex, certInfo);
- switch (status) {
- case noErr: return (TRUE);
- case kSIGIndexErr: return (FALSE);
- default: FailOSErr(status);
- }
- }
-
- /*
- * Get information about a specific attribute of a distinguished name
- * in a specific certificate of a signature. Returns TRUE if
- * successful, FALSE if an index was outside the allowable range.
- * Fail on other errors
- * (Untested)
- */
- Boolean
- CSignature::GetCertNameAttributes(
- unsigned long certIndex,
- unsigned long attributeIndex,
- SIGNameAttributesInfo *attributeInfo
- )
- {
- OSErr status;
-
- CheckForContext();
- status = SIGGetCertNameAttributes(
- gSIGContextPtr,
- certIndex,
- attributeIndex,
- attributeInfo
- );
- switch (status) {
- case noErr: return (TRUE);
- case kSIGIndexErr: return (FALSE);
- default: FailOSErr(status);
- }
- }
-
- /*
- * Setup for the status window callback.
- */
- void
- CSignature::InitDefaultStatusProc(
- ConstStr255Param actionString,
- ConstStr255Param objectString
- )
- {
- ForgetObject(itsStatusManager);
- itsStatusManager = new (SIGStatusManager);
- itsStatusManager->ISIGStatusManager(actionString, objectString);
- }
-
- void
- CSignature::DisposeDefaultStatusProc(void)
- {
- ForgetObject(itsStatusManager);
- }
-
- /*
- *** The following is a function, not a method.
- */
- /*
- * DisposeSignerContext frees memory needed by a signature context,
- * and frees up memory needed by the Digital Signature manager. You
- * should do this as soon as possible after signing or verifying a
- * file or object. The functions delete context on error, but retain
- * it (so you can extract information about the signer or certificate
- * after successful returns).
- *
- * Note: this is a global function. We ignore any erros.
- */
- void
- DisposeSignerContext(void)
- {
- SIGContextPtr aContextPtr;
-
- if ((aContextPtr = gSIGContextPtr) != NULL) {
- gSIGContextPtr = NULL;
- (void) SIGDisposeContext(aContextPtr);
- }
- }
-
-
-